package com.herui.sdyu_lib.base;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;


import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sdyu_lib.utils.NetUtils;
import com.herui.sdyu_lib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.multidex.MultiDex;

/**
 * Created by Administrator on 2016/5/9.
 */
public class BaseAppcation extends Application {

    private static BaseAppcation instance;

    public static <T extends BaseAppcation> T getInstance() {
        return (T) instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this) ;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (instance == null) {
            instance = this;
        }
        ToastUtil.setContext(this);

//        AliOssApp.getInstance().init(this);
    }

    protected void registerNetChage(){

        // 注册网络状态变化
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(myNetReceiver, mFilter);
    }


    @Override
    public void onLowMemory() {
        super.onLowMemory();
        ToastUtil.showMessage("当前应用已经内存不足，请注意。。。");
    }

    protected List<Activity> activitys = new ArrayList<Activity>(); //界面集合

    public void addActivity(Activity activity) {
        activitys.remove(activity);
        activitys.add(activity);
    }

    public void exitAllActivity() {
        for (Activity activity : activitys) {
            activity.finish();
        }
    }

    public void delActivity(Activity activity) {
        activitys.remove(activity);
    }

    public void exitActivity(Activity activity) {
        activitys.remove(activity);
        activity.finish();
    }
    public List<Activity> getActivitys() {
        return activitys;
    }

    //=======================网络状态监听========================//
    private BroadcastReceiver myNetReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            LogUtils.i("NetChageReceiver :" + NetUtils.isNetActive());
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                netChage(NetUtils.isNetActive());
            }

        }
    };

    /**
     * 网络激活
     *
     * @param netActive
     */
    protected void netChage(boolean netActive) {
        if (netActive) {
            new Thread(new Runnable() {
                @Override
                public void run() {
//                    CrashHandler.getInstance().sendExceptionAllFile();
                }
            }).start();
        }
    }
}
